<?php

namespace app\service;

use app\model\Development;
use app\traits\Singleton;
use app\utils\DataProcessor;

class DevelopmentService
{
    use Singleton;

    public function getDevelopmentList($name, $page, $pageSize)
    {
        $model = Development::where("is_delete", "=", 0)
            ->order("id", "desc");

        if ($name) {
            $model->where("name", "like", "%" . $name . "%");
        }

        $paginator = $model
            ->page($page)
            ->paginate($pageSize);

        $data = array_map(function ($item) {
            return $item;
        }, $paginator->getCollection()->toArray());

        return [
            "list" => $data,
            "total" => $paginator->total()
        ];
    }

    public function createDevelopment($name, $sex, $realName, $position, $status, $description)
    {
        return Development::create([
            'name' => $name,
            'sex' => $sex,
            'real_name' => $realName,
            'position' => $position,
            'status' => $status,
            'description' => $description
        ]);
    }

    public function updateDevelopment($id, $name, $sex, $realName, $position, $status, $description)
    {
        return Development::where("id", $id)->update([
            'name' => $name,
            'sex' => $sex,
            'real_name' => $realName,
            'position' => $position,
            'status' => $status,
            'description' => $description
        ]);
    }

    public function deleteDevelopment($id)
    {
        return Development::where("id", $id)->update([
            'is_delete' => 1,
            'delete_time' => time()
        ]);
    }

    public function batchCreateDevelopment($batchData)
    {
        $chunkSize = 5;
        $chunkData = array_chunk($batchData, $chunkSize);
        foreach ($chunkData as $chunk) {
            Development::insertAll($chunk);
        }
        return true;
    }

    public function batchDeleteDevelopment($ids)
    {
        return Development::where("id", "in", $ids)->update([
            'is_delete' => 1,
            'delete_time' => time()
        ]);
    }

    public function batchUpdateDevelopment($batchData)
    {
        $chunkSize = 5;
        $chunkData = array_chunk($batchData, $chunkSize);
        foreach ($chunkData as $chunk) {
            $updateData = [];
            foreach ($chunk as $item) {
                $updateData[] = [
                    'id' => $item['id'],
                    'name' => $item['name'],
                    'sex' => $item['sex'],
                    'real_name' => $item['real_name'],
                    'position' => $item['position'],
                    'status' => $item['status'],
                    'description' => $item['description']
                ];
            }
            // 使用 saveAll 批量更新
            (new Development())->saveAll($updateData);
        }
        return true;
    }

    public function importDevelopment($data, $fields)
    {
        $rowData = (new DataProcessor())
            ->setMappings([
                'sex' => ['男' => 1, '女' => 2],
                'status' => ['正常' => 1, '禁用' => 2]
            ])
            ->setTimeFields(['create_time', 'update_time'])
            ->process($data, $fields);
        return Development::insertAll($rowData);
    }

    public function getDevelopmentListNotPage()
    {
        return Development::where("is_delete", "=", 0)
            ->order("id", "desc")
            ->select()
            ->toArray();
    }
}